package com.wenyu.wenyuyuedu.adapter.personage;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.personage.MyCollectBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2017/3/12.
 */

public class MyCollectAdapter extends BaseAdapter {

    private Context context;
    private List<MyCollectBean.InfoBean> info = new ArrayList<>();
    private View view;
    private deleteOnclickListener deleteOnclickListener;

    public void setDeleteOnClickLis(MyCollectAdapter.deleteOnclickListener deleteOnClickLis) {
        this.deleteOnclickListener = deleteOnClickLis;

    }

    public MyCollectAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return info.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        MyCollectHolder holder = null;
        if (view == null) {
            holder = new MyCollectHolder();
            view = View.inflate(context, R.layout.mycollect_item, null);
            holder.btn_delete = (Button) view.findViewById(R.id.btn_delete);
            holder.tv = (RelativeLayout) view.findViewById(R.id.tv);
            holder.mycollect_data = (TextView) view.findViewById(R.id.mycollect_data);
            holder.mycollect_mouth = (TextView) view.findViewById(R.id.mycollect_mouth);
            holder.home_all_content = (TextView) view.findViewById(R.id.home_all_content);
            holder.contentOne = (ImageView) view.findViewById(R.id.home_all_content_picture_one);
            holder.contentTwo = (ImageView) view.findViewById(R.id.home_all_content_picture_two);
            holder.contentThree = (ImageView) view.findViewById(R.id.home_all_content_picture_three);
            holder.contentFour = (ImageView) view.findViewById(R.id.home_all_content_picture_four);
            holder.contentFive = (ImageView) view.findViewById(R.id.home_all_content_picture_five);
            holder.iv_allattention_transparency = (ImageView) view.findViewById(R.id.iv_allattention_transparency);
            holder.pictureCount = (TextView) view.findViewById(R.id.home_all_picture_count);
            holder.mycollect_circle_name = (TextView) view.findViewById(R.id.mycollect_circle_name);
            holder.mycollect_createn_name = (TextView) view.findViewById(R.id.mycollect_createn_name);
            view.setTag(holder);
        } else {
            holder = (MyCollectHolder) view.getTag();
        }
        if (info != null) {
        /*获取你年月日*/
            String create_time = info.get(i).getCreate_time();
            long l = Long.valueOf(create_time) * 1000;
            Date date = new Date(l);
        /*日*/
            int day = date.getDate();
            if(day < 10){
                holder.mycollect_data.setText( "0" + day + "");
            }else {
                holder.mycollect_data.setText( day + "");
            }

        /*月*/
            int month = date.getMonth() + 1;
            holder.mycollect_mouth.setText(month + "月");
        /*获取帖子的内容*/
            holder.home_all_content.setText(info.get(i).getContent());
            /*书圈的标题*/
            holder.mycollect_circle_name.setText(info.get(i).getTitle());
            /*书圈的发布*/
            holder.mycollect_createn_name.setText(info.get(i).getCircle_creator());
            /*帖子内容的图片*/
            List<String> images = info.get(i).getImages();
            if (images != null) {
                int size = images.size();
                if (size == 1) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    holder.contentTwo.setVisibility(View.GONE);
                    holder.contentThree.setVisibility(View.GONE);
                    holder.contentFour.setVisibility(View.GONE);
                    holder.contentFive.setVisibility(View.GONE);
                    holder.iv_allattention_transparency.setVisibility(View.GONE);
                    holder.pictureCount.setVisibility(View.GONE);
                } else if (size == 2) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    holder.contentTwo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    Glide.with(context)
                            .load(images.get(1))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentTwo);
                    holder.contentThree.setVisibility(View.GONE);
                    holder.contentFour.setVisibility(View.GONE);
                    holder.contentFive.setVisibility(View.GONE);
                    holder.iv_allattention_transparency.setVisibility(View.GONE);
                    holder.pictureCount.setVisibility(View.GONE);
                } else if (size == 3) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    holder.contentTwo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(1))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentTwo);
                    holder.contentThree.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(2))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentThree);
                    holder.contentFour.setVisibility(View.GONE);
                    holder.contentFive.setVisibility(View.GONE);
                    holder.iv_allattention_transparency.setVisibility(View.GONE);
                    holder.pictureCount.setVisibility(View.GONE);
                } else if (size == 4) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    holder.contentTwo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(1))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentTwo);
                    holder.contentThree.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(2))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentThree);
                    holder.contentFour.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(3))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentFour);
                    holder.contentFive.setVisibility(View.GONE);
                    holder.iv_allattention_transparency.setVisibility(View.GONE);
                    holder.pictureCount.setVisibility(View.GONE);
                } else if (size == 5) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    holder.contentTwo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(1))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentTwo);
                    holder.contentThree.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(2))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentThree);
                    holder.contentFour.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(3))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentFour);
                    holder.contentFive.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(4))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentFive);
                    holder.iv_allattention_transparency.setVisibility(View.GONE);
                    holder.pictureCount.setVisibility(View.GONE);
                } else if (size > 5) {
                    holder.contentOne.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(0))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentOne);
                    holder.contentTwo.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(1))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentTwo);
                    holder.contentThree.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(2))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentThree);
                    holder.contentFour.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(3))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentFour);
                    holder.contentFive.setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(images.get(4))
                            .error(R.drawable.nomal_dynamic)
                            .into(holder.contentFive);
                    holder.iv_allattention_transparency.setVisibility(View.VISIBLE);
                    holder.pictureCount.setVisibility(View.VISIBLE);
                    holder.pictureCount.setText("+" + String.valueOf(size - 5));
                }
            }
        }
        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteOnclickListener != null) {
                    deleteOnclickListener.onClick(i, view);
                }
            }
        });
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (deleteOnclickListener != null) {
                    deleteOnclickListener.onClick(i, view);
                }
            }
        });
        return view;
    }

    public void addAll(List<MyCollectBean.InfoBean> info) {
        if (info != null) {
            this.info.addAll(info);
            notifyDataSetChanged();
        }

    }

    public List<MyCollectBean.InfoBean> getList() {
        return info;
    }

    class MyCollectHolder {
        RelativeLayout tv;
        Button btn_delete;
        TextView mycollect_data;
        TextView mycollect_mouth;
        TextView home_all_content;
        ImageView contentOne, contentTwo, contentThree,
                contentFour, contentFive, iv_allattention_transparency;
        TextView pictureCount;
        TextView mycollect_circle_name;
        TextView mycollect_createn_name;
    }

    public interface deleteOnclickListener {
        void onClick(int position, View view);
    }
}
